//
//  CarDaiViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/24.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "CarDaiViewController.h"

@interface CarDaiViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>{
    NSInteger yearCount;
    NSInteger qcount;
    NSArray *array1;
    NSArray *array2;
    UIPickerView *pickerView1;
    UIPickerView *pickerView2;
    UIView *containerView;
    UIView *containerView2;
}
@property (weak, nonatomic) IBOutlet UILabel *labPrice;

@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UILabel *labBL;

@property (weak, nonatomic) IBOutlet UIView *view3;
@property (weak, nonatomic) IBOutlet UILabel *labYear;
@property (weak, nonatomic) IBOutlet UILabel *labLL;
@property (weak, nonatomic) IBOutlet UILabel *labSF;
@property (weak, nonatomic) IBOutlet UILabel *labYG;

@property (nonatomic,copy)NSString *rate;

@end

@implementation CarDaiViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self showNav:@"车贷计算器"];
    
    self.labPrice.text = _price;
    self.labSF.text = @"";
    self.labYG.text = @"";
    
    array1 = @[@"20%",@"30%",@"40%",@"50%",@"60%",@"70%",@"80%",@"90%"];
    array2 = @[@"1年（12期）",@"2年（24期）",@"3年（36期）",@"4年（48期）",@"5年（60期）"];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadPicker1)];
    _view2.userInteractionEnabled = YES;
    [_view2 addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadPicker2)];
    _view3.userInteractionEnabled = YES;
    [_view3 addGestureRecognizer:tap2];
    //利率
    [self loadData];
}
//首付比例
- (void)loadPicker1{
    containerView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - 228, ScreenWidth, 228)];
    containerView.backgroundColor = [UIColor whiteColor];
    
    UIButton *btnOK = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth -70, 5, 40, 30)];
    btnOK.titleLabel.font = [UIFont systemFontOfSize:18.0];
    [btnOK setTitle:@"确定" forState:UIControlStateNormal];
    [btnOK setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnOK addTarget:self action:@selector(pickerViewBtnOk:) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:btnOK];
    
    UIButton *btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(30, 5, 40, 30)];
    btnCancel.titleLabel.font = [UIFont systemFontOfSize:18.0];
    [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [btnCancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnCancel addTarget:self action:@selector(pickerViewBtnCancel:) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:btnCancel];
    
    pickerView1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 32, ScreenWidth, 228 - 44)];
    pickerView1.backgroundColor = [UIColor whiteColor];
    pickerView1.delegate = self;
    pickerView1.dataSource = self;
    [containerView addSubview:pickerView1];
    [pickerView1 selectRow:0 inComponent:0 animated:YES];
    containerView2.hidden = YES;
    [self.view addSubview:containerView];
    
}
//贷款年限
- (void)loadPicker2{
    containerView2 = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - 228, ScreenWidth, 228)];
    containerView2.backgroundColor = [UIColor whiteColor];
    
    UIButton *btnOK = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth -70, 5, 40, 30)];
    btnOK.titleLabel.font = [UIFont systemFontOfSize:18.0];
    [btnOK setTitle:@"确定" forState:UIControlStateNormal];
    [btnOK setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnOK addTarget:self action:@selector(pickerViewBtnOk:) forControlEvents:UIControlEventTouchUpInside];
    [containerView2 addSubview:btnOK];
    
    UIButton *btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(30, 5, 40, 30)];
    btnCancel.titleLabel.font = [UIFont systemFontOfSize:18.0];
    [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [btnCancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnCancel addTarget:self action:@selector(pickerViewBtnCancel:) forControlEvents:UIControlEventTouchUpInside];
    [containerView2 addSubview:btnCancel];
    
    pickerView2 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 32, ScreenWidth, 228 - 44)];
    pickerView2.backgroundColor = [UIColor whiteColor];
    pickerView2.delegate = self;
    pickerView2.dataSource = self;
    [containerView2 addSubview:pickerView2];
    
    [pickerView2 selectRow:0 inComponent:0 animated:YES];
    containerView.hidden = YES;
    [self.view addSubview:containerView2];
}

- (void)pickerViewBtnOk:(UIButton *)btn{
    [containerView removeFromSuperview];
    [containerView2 removeFromSuperview];
}

- (void)pickerViewBtnCancel:(UIButton *)btn{
    [containerView removeFromSuperview];
    [containerView2 removeFromSuperview];
}
#pragma -mark UIPickerViewDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView == pickerView1) {
        return array1.count;
    }
    return array2.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView == pickerView1) {
        return array1[row];
    }
    return array2[row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (pickerView == pickerView1) {
        _labBL.text = array1[row];
        
    }
    _labYear.text = array2[row];
}

//计算
- (IBAction)clickJS:(id)sender {
    /*
     车贷计算公式：[{（价格-价格*首付比例）*利率*5/1.9}+（价格-价格*首付比例）]/贷款期数=月供
     20-90
     */
    if ([_labYear.text isEqualToString:@"1年（12期）"]) {
        yearCount = 1;
        qcount = 12;
    }else if ([_labYear.text isEqualToString:@"2年（24期）"]){
        yearCount = 2;
        qcount = 24;
    }else if ([_labYear.text isEqualToString:@"3年（36期）"]){
        yearCount = 3;
        qcount = 36;
    }else if ([_labYear.text isEqualToString:@"4年（48期）"]){
        yearCount = 4;
        qcount = 48;
    }else{
        yearCount = 5;
        qcount = 60;
    }
    
    CGFloat price = [_price floatValue]*10000.0;
    CGFloat bl = [_labBL.text floatValue]/100.0;
    
    CGFloat rate = [_rate floatValue];
    CGFloat lilv = rate/100;
    
    CGFloat a = (price-price*bl);
    CGFloat b = (lilv*yearCount/1.895+1);
    CGFloat yg = (a * b)/qcount;
    NSLog(@"price:%.2f-bl:%.2f-a:%.2f-b:%.2f-yg:%.2f",price,bl,a,b,yg);
    CGFloat sf = price * bl;
    _labSF.text = [NSString stringWithFormat:@"%.2f元",sf];
    _labYG.text = [NSString stringWithFormat:@"%.2f元",yg];
    
}
//sure
- (IBAction)clickSure:(id)sender {
    if (_labSF.text.length == 0) {
        
        return;
    }
    
    [self.delegate sendPrice1:_labSF.text Price2:_labYG.text];
    [self.navigationController popViewControllerAnimated:YES];
}
//利率
- (void)loadData{
    NSString *url = [NSString stringWithFormat:@"%@/App/CarBuying/rate",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        _rate = [[responseObject objectForKey:@"result"] objectForKey:@"rate"];
        NSString *percen = @"%";
        _labLL.text = [NSString stringWithFormat:@"%@%@",_rate,percen];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
